package com.lasertd.model;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;

public class Area {

	private final static int XSIZE = 16;
	private static int YSIZE = Area.XSIZE;
	
	private Array<Field> area = new Array<Field>();
	
	
	public Area() {
		this.createEmptyArea();
	}
	
	public Array<Field> getFields() {
		return this.area;
	}
	
	public Field getField(Vector2 v) {
		for (Field field : this.area) {
			if(field.getPos().equals(v))
			{
				return field;
			}
		}
		return null;
	}
	
	private void createEmptyArea() {
		
		for(int i = 0; i < Area.XSIZE; i++)
		{
			for(int j = 0; j < Area.YSIZE; j++)
			{
				this.area.add(new Field( new Vector2(i, j), true, false ));
			}
		}
		
	}
	
	public void createBorder() {
		for(Field f : this.area)
		{
			if(f.getPos().x == 0 || f.getPos().x == Area.XSIZE - 1 || f.getPos().y == 0 || f.getPos().y == Area.YSIZE - 1)
			{
				f.setBuildable(false);
				f.setPassable(false);
			}
				
		}
	}
	
	public void createRandomPath(Vector2 start, Vector2 end) {
		if(this.getField(start) != null && this.getField(end) != null) {
			Field startField = this.getField(start);
			Field endField = this.getField(end);
			
			startField.setStart(true);
			endField.setEnd(true);
			
			this.randomNextField(startField);
		}
	}
	
	private void randomNextField(Field f) {
		// left way
		int newX = (int) (f.getPos().x - 1);
		Field newField = this.getField(new Vector2(newX, f.getPos().y));
		if( newField != null && newField.isBuildable() ) {
			newField.setPassable(true);
			
			this.randomNextField(newField);
		}
	}
	
}
